package login;

import io.ClientSession;
import io.OperationResponse;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

/**
 *
 * @author zhengjiangyu
 *
 */
public class I4login {
	protected static final Logger log = LoggerFactory.getLogger(XYlogin.class);

//	Key:8148112b941e470f8bcbe72532e82091
	static String  serverUrl="https://pay.i4.cn/member_third.action";
	static String appid="341";
	public static void Login(OperationResponse operationRequest,
			ClientSession session) throws ClientProtocolException, IOException, JSONException {
		if (operationRequest.Parameters.size() < 2) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, null, ReturnCode.No, "");
			session.SendOperationResponse(response);
		}
//		String id="2454026_5519vok1mmgl8j0d03h6l0idj6";
		String id = (String) operationRequest.Parameters
				.get(ParameterType.UserID);
		String token = (String) operationRequest.Parameters
				.get(ParameterType.UserPwd);
		String deviceID = (String) operationRequest.Parameters
				.get(ParameterType.DeviceID);
		String isGetIP = (String) operationRequest.Parameters
				.get(ParameterType.IsGetIP);
		String PhoneType=(String) operationRequest.Parameters
				.get(ParameterType.PhoneType);
		
		if (id.equals("zealm")&&token.equals("")) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, AppConfig.dicAppInfo,
					ReturnCode.GetServer, "");
			session.SendOperationResponse(response);
			return;

		}
		
		
	
		id="token="+id;
		id = new String(id.getBytes("gbk"), "utf-8");
//		HttpsUtil.post(serverUrl, body);
		String result = HttpClient.sendXMLDataByPost(serverUrl, id);
		if(!result.equals("")){
		JSONObject date=new JSONObject(result);
		String status=date.optString("status");
		if(status.equals("0")){
			String userid=date .optString("userid");
			id="i4"+userid;
			operationRequest.Parameters.put(ParameterType.UserID, id);
			HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
			myPrams.put(ParameterType.UserID, id);
			OperationResponse responseID = new OperationResponse(
					operationRequest.OperationCode, myPrams,
					ReturnCode.HasID, "");
			session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session,id, deviceID, isGetIP,"I4",PhoneType,"");
		}else{
			return;
		}
		}else {
			log.info("i4 return have a error result ="+result);
		}
	}
	public  Runnable getLoginIS(OperationResponse operationResponse, ClientSession session){ 
		class LoginIS implements Runnable { 
			private OperationResponse t_operationResponse;
			private ClientSession t_session;
			public LoginIS(OperationResponse operationResponse, ClientSession session)
			{
				t_operationResponse = operationResponse;
				t_session = session;
			}
			
			@Override
			public void run() {
					try {
						Login(t_operationResponse, t_session);
					} catch (Exception e) {
						log.info(Thread.currentThread().getName() + "i4Login task Error!!!");
					}
				}
        }
	    return new LoginIS(operationResponse, session); 
	 } 

}
